package Vibrar;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
 
/**
 *
 * @author Barna
 */
 
public class Vibration
{
    //frame
    static JFrame frame = new JFrame("Vibration");
    static JPanel panel = new JPanel();
    static JButton button = new JButton("Click me");
 
    // the vibrating method, TODO later in separate class
    // initial amplitude (pixel), release time (ms), velocity (ms)
    // release has linear characteristic
    private static void vibrate(
            int maxAmplitude,
            int releaseTime,
            int velocity,
            JComponent component)
    {
        // get sctual location
        Point initialPosition = component.getLocation();
        double randomAngle, randomDistance;
        System.out.println("Vibrating " + initialPosition.x + " " + initialPosition.y);
 
        // FOR cycle releaseTime/velocity
        for(double timeRemaining = (double) releaseTime;
                timeRemaining >= 0;
                timeRemaining -= (double) velocity)
        {
            // get one vibration
            // angle in radian! 360 degrees = 2*Math.PI
            randomAngle = Math.random()*2*Math.PI;
            randomDistance = Math.random() * maxAmplitude * timeRemaining / releaseTime;
 
            // modify location
            component.setLocation(initialPosition.x + (int) (Math.cos(randomAngle)*randomDistance),
                    initialPosition.y + (int) (Math.sin(randomAngle)*randomDistance));
        }
        // end of vibration, reset location
        component.setLocation(initialPosition);
    }
 
    static class buttonViewer implements ActionListener
    {
        public void actionPerformed(ActionEvent e) {
        	vibrate (100, 1000000, 1, button);
        }
    }
 
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        frame.setSize(300, 200);
        // add button to panel
 
        panel.add(button);
        frame.add(panel);
 
        button.setSize(50, 50);
//        button.setLocation((frame.getWidth()-button.getWidth())-2,
//                (frame.getHeight()-button.getHeight())/2);
 
        button.setLocation(100, 100);
 
        System.out.println(frame.getWidth() + " " + frame.getHeight());
        System.out.println(panel.getWidth() + " " + panel.getHeight());
        System.out.println(button.getWidth() + " " + button.getHeight());
 
        // show the frame
        Dimension frameDimension = frame.getSize();
        Dimension ScreenSize = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation(
                (ScreenSize.width-frameDimension.width)/2,
                (ScreenSize.height-frameDimension.height)/2
                );
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        
        button.setText("Vibrating");
        vibrate (10, 1000000, 1, button);
        button.setText("Click me");
        button.addActionListener(new buttonViewer());
        
    }
}